package phoenix.core.helpers.extensions;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.TestException;

import phoenix.core.PhoenixManager;
import core.ApplicationManager;
import core.helpers.MobDriverHelper;

public class MobDriverHelperExt extends MobDriverHelper
{
	// PhoenixManager instance
	protected static PhoenixManager phoenix;
	private static WebDriverWait driverWait;

	/**
	 * Initializes WebDriverHelperExt
	 * 
	 * @param p_manager
	 *            ApplicationManager instance
	 * @param p_phoenix
	 *            PhoenixManager instance
	 */
	public MobDriverHelperExt(ApplicationManager p_manager, PhoenixManager p_phoenix)
	{
		super(p_manager);
		phoenix = p_phoenix;
	}

	@Override
	public void click(By p_locator)
	{
		WebElement p_target = null;

		try
		{
			p_target = driver.findElement(p_locator);
			if (p_target != null)
			{
				manager.wd().click(p_locator);
			}
		}
		catch (Exception p_ex)
		{
			manager.log().warn("Unable to Touch element : '" + p_locator + "'. Exception : " + p_ex.toString());

		}
	}

	/**
	 * Scrolls to web element if needed
	 * 
	 * @param p_webElement
	 *            Web element
	 */
	public void scrollTo(WebElement p_webElement)
	{
		try
		{
			phoenix.log().info("Scroll to web element: " + p_webElement);
			phoenix.js().execute("arguments[0].scrollIntoView(true);", p_webElement);
		}
		catch (Exception p_ex)
		{
			throw new TestException("Cannot scroll to web element => " + p_webElement + "\n" + p_ex);
		}
	}
}